#include <iostream>
#include <random>
#include <sstream>
#include <iomanip>
#include <atomic>

std::string uuid()
{
    std::random_device rd;
    std::mt19937_64 generator(rd());
    std::uniform_int_distribution<int> distribution(0, 255);
    std::stringstream ss;
    for (int i = 0; i < 8; i++)
    {
        // setw设置占位2个，setfill补全占位为0, hex表示十六进制表示
        ss << std::setw(2) << std::setfill('0') << std::hex << distribution(generator);
        if (i == 3 || i == 5 || i == 7)
            ss << "-";
    }

    static std::atomic<size_t> seq(1);
    size_t num = seq.fetch_add(1);
    // fetch_add调用后，num返回原先seq的值，是1，而seq的值变成2
    for (int i = 7; i >= 0; i--)
    {
        // 0 00 000 0000 00000 000000 0000000 00000001
        ss << std::setw(2) << std::setfill('0') << (num >> (i * 8) & 0xff);
        if (i == 6)
            ss << "-";
    }
    return ss.str();
}

int main()
{
    for (int i = 0; i < 20; i++)
        std::cout << uuid() << std::endl;

    return 0;
}
